import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ConfigService } from '@nestjs/config';
import { Observable } from 'rxjs';
import { Reflector } from '@nestjs/core';

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') implements CanActivate {
  constructor(
    private readonly configService: ConfigService,
    private readonly reflector: Reflector,
  ) {
    super();
  }

  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    // 检查是否启用了JWT认证
    const jwtAuthEnabled = this.configService.get<boolean>(
      'ENABLE_JWT_AUTH',
      false,
    );
    console.log('ENABLE_JWT_AUTH:', jwtAuthEnabled);
    // 如果JWT认证被禁用，则允许所有请求通过
    if (jwtAuthEnabled === false) {
      console.log('JWT认证已禁用，允许请求通过');
      return true;
    }
    return true;
    // 否则执行标准的JWT认证
    console.log('JWT认证已启用，执行标准认证流程');
    return super.canActivate(context);
  }
}
